<?php
    require_once dirname(__FILE__) . '/shared/config/config.php';
    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'check_user_login.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_matches.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_bet_calculators.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_security.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_affiliate.php';
    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'html_generator.php';
    $title = 'Home Page';
    
    $username_main = $_SESSION['user_info']['username'];
    $match_id = filter_input(INPUT_GET, "match_id");
    
    $user_main = api_security::get_user_by_username($username_main);
    
    $error = "";
    $info = "";
    
    if (empty($match_id)) {
        $match_id = filter_input(INPUT_POST, "hidd_match_id");
        if (!empty($match_id) && $match_id > 0) {
            $choice_id = filter_input(INPUT_POST, "ddl_choice");
            $bet_amount = filter_input(INPUT_POST, "txt_amount");
            if (empty($bet_amount)) {
                $bet_amount = 100; // default
            }
            $affiliate_email = filter_input(INPUT_POST, "txt_affiliate_email");
            if (api_bet_calculators::can_bet($username_main, $bet_amount)) {
                if (!empty($affiliate_email) && filter_var($affiliate_email, FILTER_VALIDATE_EMAIL)) {
                    $info = "Betting Successful with $".$bet_amount.". Proof of bet is sent to your client ($affiliate_email)";
                    api_bet_calculators::place_bet($username_main, $bet_amount, $match_id, $choice_id, $affiliate_email);
                    $affiliate_url = get_affiliate_link($user_main['id'], $affiliate_email);
                    api_affiliate::send_mail_for_affiliate_betting($affiliate_email, $affiliate_url, $bet_amount);
                } else {
                    $info = "Betting Successful with $".$bet_amount.".";
                    api_bet_calculators::place_bet($username_main, $bet_amount, $match_id, $choice_id, "");
                }
            } else {
                $error = "Your balance must be more than $" . $bet_amount;
            }
        } else {
            $match_id = 0;
        }
    }
    
    
?>

<!DOCTYPE html>
<!-- IE SECTION -->

<!--[if lt IE 7]>
<html class = "no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>
<html class = "no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>
<html class = "no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class = "no-js"> <!--<![endif]-->

                       <!-- END IE SECTION -->
<html>
<head>
    <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php' ?>
    <!--<script type = "text/javascript" src = "<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_JS . 'ts.js' ?>"></script>-->
    <script type="text/javascript">
        var betLoading = new BetAutoLoading(
                    1000,
                    '<?php echo LINK_ROOT . DIR_AJAX; ?>load_list_bet_placed.php',
                    '<?php echo $match_id; ?>',
                    '#bet_table',
                    0,
                    '<?php echo LINK_ROOT . DIR_AJAX; ?>loading_profit_if_win.php',
                    '#popup_confirm_before_betting_message',
                    '#popup_confirm_before_betting',
                    'form[name="form_bet"]',
                    '<?php echo LINK_ROOT . DIR_AJAX; ?>delete_betting.php');
            
        $(document).ready(function() {
            betLoading.beginAutoLoad();
        });
    </script>
</head>
<body>
<?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'header.php' ?>

<!-- Title -->
<div class = "section section-black">
    <div class = "container">
        <div class = "row">
            <div class = "col-md-12">
                <div class = "calltoaction-wrapper">
                    <h3>Match Detail</h3>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- End Title -->

<!-- Match Detail -->
<div class = "section">
    <div class = "container">
        <div class = "row">

            <div class = "col-md-offset-2 col-md-8">
                <div class = "service-wrapper">
                    <form class = "form-horizontal" name="form_bet" role = "form" method="POST" action="?">
                        <?php
                        $item = api_matches::get_match_by_id($match_id);
                        ?>
                        <input type="hidden" name="hidd_match_id" value="<?php echo $item['id']; ?>"/>
                        
                        <img class = "text-left" src = "<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES_ICONS . 'coins.png' ?>" alt = "Match">

                        <?php 
                        show_match_status_message($item['is_cancel'], $item['cancel_reason'], 
                                $item['win_choice_id'], "The match's winner is '" . $item['win_choice_name'] . "'");
                        show_message($info, $error, 0, 0);
                        ?>
                        
                        <div class = "form-group ts-margin-top">
                            <label class = "col-sm-6 control-label">Name</label>

                            <div class = "col-sm-6 text-left">
                                <p class = "form-control-static"><?php echo $item['name']; ?></p>
                            </div>
                        </div>
                        <div class = "form-group ts-margin-top">
                            <label class = "col-sm-6 control-label">Description</label>

                            <div class = "col-sm-6 text-left">
                                <p class = "form-control-static"><?php echo $item['description']; ?></p>
                            </div>
                        </div>
                        <div class = "form-group">
                            <label class = "col-sm-6 control-label">Date Start</label>

                            <div class = "col-sm-6 text-left">
                                <p class = "form-control-static"><?php echo show_datetime($item['date_start']); ?></p>
                            </div>
                        </div>
                        <div class = "form-group">
                            <label class = "col-sm-6 control-label">Date End</label>

                            <div class = "col-sm-6 text-left">
                                <p class = "form-control-static"><?php echo show_datetime($item['date_end']); ?></p>
                            </div>
                        </div>
                        <div class = "form-group">
                            <label class = "col-sm-6 control-label">Remaining Time</label>

                            <div class = "col-sm-6 text-left">
                                <p class = "form-control-static ts-red">-<!--2 days, 4 hours, 15 minutes, 10 seconds--></p>
                            </div>
                        </div>
                        <div class = "form-group">
                            <label class = "col-sm-6 control-label">Country</label>

                            <div class = "col-sm-6 text-left">
                                <p class = "form-control-static"><?php echo $item['country_name']; ?></p>
                            </div>
                        </div>
                        <div class = "form-group">
                            <label class = "col-sm-6 control-label">Industry</label>

                            <div class = "col-sm-6 text-left">
                                <p class = "form-control-static"><?php echo $item['industry_name']; ?></p>
                            </div>
                        </div>
                        <?php 
                        $disabled = "";
                        if (!api_matches::is_still_live($match_id)) {
                            $disabled = "disabled";
                        }
                        ?>
                        <div class = "form-group">
                            <label class = "col-sm-6 control-label">Choices</label>

                            <div class = "col-sm-4 text-left">
                                <select name = "ddl_choice" class = "form-control" <?php echo $disabled; ?>>
                                    <?php
                                    $choices = api_matches::get_match_choices_by_match_id($item['id']);
                                    foreach ($choices as $choice) {
                                    ?>
                                    <option value="<?php echo $choice['choice_id']; ?>"><?php echo $choice['choice_name']; ?></option>
                                    <?php
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class = "form-group">
                            <label class = "col-sm-6 control-label">Amount</label>

                            <div class = "col-sm-4 text-left">
                                <div class = "input-group">
                                    <span class = "input-group-addon">$</span>
                                    <input name = "txt_amount" type = "text" class = "form-control" placeholder = "100" <?php echo $disabled; ?>>
                                </div>
                            </div>
                        </div>
                        <?php
                        if (!empty($user_main['is_affiliate'])
                                && $user_main['is_affiliate'] == 1) {
                        ?>
                        <div class = "form-group">
                            <label class = "col-sm-6 control-label">Email <span class="ts-red"> (if user registered affiliate)</span></label>

                            <div class = "col-sm-4 text-left">
                                <input name = "txt_affiliate_email" type = "text" class = "form-control" placeholder = "email@domain.com" <?php echo $disabled; ?>>
                            </div>
                        </div>
                        <?php 
                        }
                        
                        if (api_matches::is_still_live($match_id)) {
                        ?>
                        <input name = "btn_bet" onclick="betLoading.showPopupProfitIfWin();" type = "button" class = "btn" value = "Place Bet">
                        <?php
                        } else {
                        ?>
                        <div class="alert alert-danger">
                            Expired match.
                        </div>
                        <?php
                        }
                        ?>
                    </form>

                </div>
            </div>

        </div>
        <div id="bet_table" class = "row">

            
        </div>

    </div>
</div>
<!-- End Match Detail -->

<?php echo show_modal_betting_confirmation("popup_confirm_before_betting", "betLoading.submitBetting();"); ?>

<?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php' ?>
</body>
</html>